﻿using UnityEngine;
using System.Collections;

public class TraitSelPaper : MonoBehaviour
{

    UnitStatus m_SelUnitStatus;
    GameObject m_ObjSelPossibleBox;

    // Use this for initialization
    void Start()
    {
        transform.localScale = new Vector3(0.6f, 0.25f);
        m_SelUnitStatus = transform.parent.GetComponent<UnitNoteDetails>().unitStatus;

        PointCheck();
        ClassTraitInit();
    }

    public void PointCheck()
    {
        transform.FindChild("RemainPoint").GetComponent<UILabel>().text = string.Format("Remaining Point : {0}", m_SelUnitStatus.m_unitData.iTraitPoint);

        if (GameObject.Find("SelectPossible(Clone)"))
            Destroy(GameObject.Find("SelectPossible(Clone)").gameObject);

        for (int i = 1; i < 7; ++i)
        {
            transform.GetChild(i).GetComponent<SelectableTraitSeal>().m_bSelectable = false;
        }

        if (m_SelUnitStatus.m_unitData.iTraitPoint > 0)
        {
            for (int i = 0; i < m_SelUnitStatus.m_unitData.arrayTrait.Length; ++i)
            {
                if (m_SelUnitStatus.m_unitData.arrayTrait[i].m_strSpriteName == "")
                {
                    switch (i)
                    {
                        case 1:
                            transform.GetChild(1).GetComponent<SelectableTraitSeal>().m_bSelectable = true;
                            transform.GetChild(2).GetComponent<SelectableTraitSeal>().m_bSelectable = true;
                            CreateSelBrightBox(new Vector3(-30.25f, 0), 1, 2);
                            break;
                        case 2:
                            transform.GetChild(3).GetComponent<SelectableTraitSeal>().m_bSelectable = true;
                            transform.GetChild(4).GetComponent<SelectableTraitSeal>().m_bSelectable = true;
                            CreateSelBrightBox(Vector3.zero, 3, 4);
                            break;
                        case 3:
                            transform.GetChild(5).GetComponent<SelectableTraitSeal>().m_bSelectable = true;
                            transform.GetChild(6).GetComponent<SelectableTraitSeal>().m_bSelectable = true;
                            CreateSelBrightBox(new Vector3(30, 0), 5, 6);
                            break;

                        default:
                            transform.FindChild("RemainPoint").gameObject.SetActive(false);
                            Debug.LogError("TraitError");
                            break;
                    }

                    break;
                }
            }
        }
    }

    void CreateSelBrightBox(Vector3 localPos, int i, int j)
    {
        m_ObjSelPossibleBox = Instantiate(Resources.Load("Prefaps/UI/Paper/TraitSelect/SelectPossible") as GameObject) as GameObject;
        m_ObjSelPossibleBox.transform.parent = transform;
        m_ObjSelPossibleBox.transform.localPosition = localPos;
        m_ObjSelPossibleBox.transform.localScale = new Vector3(0.25f, 1.25f);

        transform.GetChild(i).GetComponent<SelectableTraitSeal>().m_bSelectable = true;
        transform.GetChild(j).GetComponent<SelectableTraitSeal>().m_bSelectable = true;
    }

    // Update is called once per frame
    void Update()
    {

    }


    void ClassTraitInit()
    {
        switch (m_SelUnitStatus.m_unitData.ClassStatus.enClass)
        {
            case CLASS_TYPE.TECHNICIAN:
                switch (m_SelUnitStatus.m_unitData.ClassStatus.AssnStatus.enAssnType)
                {
                    case ASSN_TYPE.DEFAULT:
                        AlignTraitsPerClass("Engineering", "mine", "ComingSoon", "ComingSoon", "ComingSoon", "ComingSoon");
                        break;

                    case ASSN_TYPE.RANGE:
                        AlignTraitsPerClass("medical", "medicFight", "ComingSoon", "ComingSoon", "ComingSoon", "ComingSoon");
                        break;

                    case ASSN_TYPE.CLOSE:
                        break;
                }
                break;

            case CLASS_TYPE.POLICE:
                switch (m_SelUnitStatus.m_unitData.ClassStatus.AssnStatus.enAssnType)
                {
                    case ASSN_TYPE.DEFAULT:
                        RemoveTraits();
                        break;

                    case ASSN_TYPE.RANGE:
                        AlignTraitsPerClass("sharpshooter", "footShooter", "ComingSoon", "ComingSoon", "ComingSoon", "ComingSoon");
                        break;

                    case ASSN_TYPE.CLOSE:
                        AlignTraitsPerClass("shotgun", "muscle", "ComingSoon", "ComingSoon", "ComingSoon", "ComingSoon");
                        break;
                }
                break;

            case CLASS_TYPE.SOLDIER:
                switch (m_SelUnitStatus.m_unitData.ClassStatus.AssnStatus.enAssnType)
                {
                    case ASSN_TYPE.DEFAULT:
                        RemoveTraits();
                        break;

                    case ASSN_TYPE.RANGE:
                        AlignTraitsPerClass("sniper", "Grenade", "ComingSoon", "ComingSoon", "ComingSoon", "ComingSoon");
                        break;

                    case ASSN_TYPE.CLOSE:
                        AlignTraitsPerClass("GrenadeBullet", "moraleDown", "ComingSoon", "ComingSoon", "ComingSoon", "ComingSoon");
                        break;
                }
                break;
        }
    }


    void AlignTraitsPerClass(string Top0, string Bot0, string Top1, string Bot1, string Top2, string Bot2)
    {
        DataManager m_dataMgr;
        m_dataMgr = GameObject.Find("DataLogic").GetComponent<DataManager>();

        transform.GetChild(1).GetComponent<SelectableTraitSeal>().m_Trait = m_dataMgr.GetTraitData(Top0);
        transform.GetChild(2).GetComponent<SelectableTraitSeal>().m_Trait = m_dataMgr.GetTraitData(Bot0);
        transform.GetChild(3).GetComponent<SelectableTraitSeal>().m_Trait = m_dataMgr.GetTraitData(Top1);
        transform.GetChild(4).GetComponent<SelectableTraitSeal>().m_Trait = m_dataMgr.GetTraitData(Bot1);
        transform.GetChild(5).GetComponent<SelectableTraitSeal>().m_Trait = m_dataMgr.GetTraitData(Top2);
        transform.GetChild(6).GetComponent<SelectableTraitSeal>().m_Trait = m_dataMgr.GetTraitData(Bot2);

        for (int i = 1; i < 7; ++i)
        {
            transform.GetChild(i).GetComponent<SelectableTraitSeal>().InitSeal();
        }
    }


    void RemoveTraits()
    {
        for (int i = 1; i < 7; ++i)
        {
            Destroy(transform.GetChild(i).gameObject);
        }
        transform.FindChild("RemainPoint").GetComponent<UILabel>().text = string.Format("{0} can't add traits", m_SelUnitStatus.m_unitData.ClassStatus.AssnStatus.strAssnName);
    }
}
